<!--
 * @Descripttion: 盘点单列表
 * @version:
 * @Author: cxguo
 * @Date: 2019-08-02 15:03:05
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-12 15:37:52
 -->
<template>
  <div class="app-container">
    <div class="clearfix">
      <el-card shadow="never" class="oper-btn-card">
        <div class="clearfix">
          <el-form
            class="fl form-no-bottom"
            :inline="true"
            :model="searchData"
            label-position="left"
            label-width="80px"
            @keydown.enter.native="btnSearch"
          >
            <el-form-item>
              <el-input
                v-model="searchData.keywords"
                clearable
                style="width: 260px"
                placeholder="按单据号，商品名，查询."
              />
              <el-button type="primary" :loading="btnLoading.search" @click="btnSearch()">
                搜索
              </el-button>
              <el-checkbox
                v-model="searchData.isCancle"
                true-label="0"
                false-label="1"
                border
                type="primary"
                @change="()=>{listData()}"
              >
                不显示已作废单据
              </el-checkbox>
              <el-button type="default" @click="isFilterOpen = !isFilterOpen">
                {{ btnFilterOpenText }}
              </el-button>
            </el-form-item>
          </el-form>
          <div class="fr">
            <el-button :type="storeBtnType" @click="btnHandleLock">{{ storeStatus }}</el-button>
            <el-button type="primary" @click="btnAddData">新增盘点单</el-button>
          </div>
        </div>
        <SearchFilter
          v-show="isFilterOpen"
          :search="btnFilterSearch"
          :clear="btnClearConditions"
        >
          <Conditions ref="conditions" :query-data="searchData" />
        </SearchFilter>
      </el-card>
    </div>
    <div class="margin-t2">
      <vxe-grid
        ref="table"
        class="grid"
        border
        resizable
        highlight-hover-row
        :row-class-name="(({row}) => { return row.isCancle === '1' ? 'is-cancle' : ''})"
        :loading="tableLoading.table"
        :columns="columnsData"
        :data="tableData.table"
      />
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="pageInfo.rows"
        :page-sizes="[20,30,40]"
        :current-page.sync="pageInfo.page"
        :total="pageInfo.total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import { oftenuse } from '@/libs/mixins'
import { listInventory as listData, cancleBill } from '@/api/bill/bill-inventory.js'
import { getDelButton, getMoreButton, getCopyButton } from '@/components/Table/oper-btns.js'
import SearchFilter from '@/views/components/SearchFilter'
import Conditions from './conditions'
import { getBusinessTime } from '@/libs/util.js'

export default {
  name: 'CheckstockList',
  filters: {
  },
  components: { SearchFilter, Conditions },
  mixins: [oftenuse],
  data() {
    return {
      searchData: { isCancle: '0', dateTab: 3, inventoryStatus: '-1' },
      optionsData: {},
      storeData: [],
      isShowStoreLockModel: false,
      isFilterOpen: false,
      columnsData: [
        { title: '操作', width: 120,
          slots: {
            default: (params) => {
              const { row } = params
              const { isCancle } = row
              const h = this.$createElement
              const viewMoreNode = getMoreButton(h, { on: { click: () => { this.btnViewData(row) } }})
              const copyNode = getCopyButton(h, {
                props: { type: 'default' },
                attrs: { title: '复制并新增' },
                on: { click: () => { this.btnCopyAddData(row) } }
              })
              const delBtnNode = isCancle === '0'
                ? getDelButton(h, { on: { click: () => { this.btnCancleBill(row) } }})
                : null
              const divNode = h('div', {
                attrs: {
                  class: 'table-oper-btns'
                }
              }, [viewMoreNode, copyNode, delBtnNode])
              return [divNode]
            }
          }
        },
        { field: 'businessTime', title: '业务日期', width: 100,
          slots: {
            default: (params) => {
              const { row, column } = params
              return [getBusinessTime(row[column.property])]
            }
          }
        },
        { field: 'billNo', title: '单据编号', width: 140 },
        { field: 'goodsNamestr', title: '盘点商品', showOverflow: true },
        { field: 'inventoryStatus', title: '盘点状态', width: 120,
          slots: {
            default: (params) => {
              const { row } = params
              const { bookQty, realQty } = row
              const value = Number(bookQty) !== Number(realQty) ? '有盈亏' : '无盈亏'
              return [<span>{value}</span>]
            }
          }
        },
        { field: 'amountProfit', title: '盘亏金额', width: 100,
          slots: {
            default: (params) => {
              const { row } = params
              const { bookQty, realQty } = row
              const disStr = this.$amount(realQty).subtract(bookQty).format()
              const dis = Number(disStr)
              const value = dis > 0 ? '' : disStr
              return [<span>{value}</span>]
            }
          }
        },
        { field: 'amountLoss', title: '盘盈金额', width: 100,
          slots: {
            default: (params) => {
              const { row } = params
              const { bookQty, realQty } = row
              const disStr = this.$amount(bookQty).subtract(realQty).format()
              const dis = Number(disStr)
              const value = dis > 0 ? '' : disStr
              return [<span>{value}</span>]
            }
          }
        }
      ]
    }
  },
  computed: {
    'btnFilterOpenText': function() {
      return this.isFilterOpen ? '收起筛选' : '展开筛选'
    },
    'storeStatus': function() {
      const storeData = this.storeData
      const lockData = storeData.filter(item => { return item.isLock === '1' })
      const allLen = storeData.length
      const lockLen = lockData.length
      if (lockLen > 0) return `盘点后解锁(${lockLen}/${allLen})`
      return '盘点后解锁'
    },
    'storeBtnType': function() {
      const storeData = this.storeData
      const lockData = storeData.filter(item => { return item.isLock === '1' })
      const lockLen = lockData.length
      if (lockLen > 0) return 'danger'
      return 'success'
    }
  },
  watch: {
  },
  created() {
    this.listData()
    this.getStoreData()
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (vm.$route.params.refresh) {
        vm.listData()
      }
    })
  },
  methods: {
    btnHandleLock() {
      this.isShowStoreLockModel = true
    },
    btnAddData() {
      this.$router.push({
        name: 'CheckstockAdd'
      })
    },
    btnCopyAddData(row) {
      const { id } = row
      const params = { billId: id }
      this.$router.push({
        name: 'CheckstockCopyAdd',
        query: params
      })
    },
    btnViewData(row) {
      const { id } = row
      const params = { billId: id }
      this.$router.push({
        name: 'CheckstockDetail',
        query: params
      })
    },
    btnSearch() {
      this.setBtnLoading('search', true)
      this.pageInfo.current = 1
      this.listData().then(res => {
        this.setBtnLoading('search', false)
      }).catch(err => {
        console.log(err)
        this.setBtnLoading('search', false)
      })
    },
    btnClearConditions() {
      this.$refs.conditions.clearData()
    },
    btnFilterSearch() {
      this.setBtnLoading('search', true)
      this.pageInfo.current = 1
      const searchData = this.$refs.conditions.searchData
      const params = {
        data: searchData,
        page: this.pageInfo
      }
      this.listData(params).then(res => {
        this.setBtnLoading('search', false)
      }).catch(err => {
        this.$message.error('查询异常:', err.message)
        this.setBtnLoading('search', false)
      })
    },
    btnCancleBill(row) {
      this.isAllowDel('确定要作废该单据吗？').then(res => {
        this.cancleBill(row.id)
      })
    },
    btnDetailData(index, row) {
      const purchaseId = row.id
      this.jump2purchaseDetail({ purchaseId, operation: 'update' })
    },
    // api
    listData(params = { data: this.searchData, page: this.pageInfo }) {
      if (params.data.date) {
        const data = params.data
        data.beginDate = data.date[0]
        data.endDate = data.date[1]
      }
      this.setTableLoading('table', true)
      return new Promise((resolve, reject) => {
        listData(params).then(res => {
          this.setTableLoading('table', false)
          if (!res.data.flag) return
          const { records, total } = res.data.data
          this.setTableData('table', records)
          this.pageInfo.total = total
          resolve(res)
        }).catch(err => {
          reject(err)
          this.setTableLoading('table', false)
        })
      })
    },
    cancleBill(id) {
      return cancleBill(id).then(res => {
        if (res.data.flag) {
          this.listData()
          this.$notify.success(res.data.message)
        } else this.$notify.error({ title: '作废失败', message: res.data.message || '' })
      }).catch(err => {
        this.$notify.error({ title: '作废异常！', message: err })
      })
    },
    getStoreData() {
    },
    // on
    onSelectionChange(selectionData) {
      this.setTableSelectionData('customer', selectionData)
    },
    onSizeChange(val) {
      this.pageInfo.current = 1
      this.pageInfo.size = val
      this.listData()
    },
    onCurrentChange(val) {
      this.pageInfo.current = val
      this.listData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">

</style>
